﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tunnel
{
    public partial class AlarmForm : Form
    {
        public AlarmForm()
        {
            InitializeComponent();
        }

        private void AlarmForm_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            date1.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd ") + "00:00:00");
            date2.Value = DateTime.Now;

            string sql = "SELECT     H_AlarmInfo.AlarmID,H_AlarmInfo.AlarmTime, H_AlarmInfo.AlarmInfo, M_Device.DeviceID, M_Device.Name, M_Device.TypeID, M_Device.DevicePos, M_DeviceType.Name AS TypeName,H_AlarmInfo.Deal, M_User.Name AS UserName, H_AlarmInfo.DealTime FROM H_AlarmInfo LEFT OUTER JOIN M_Device ON H_AlarmInfo.DeviceID = M_Device.DeviceID LEFT OUTER JOIN M_DeviceType ON M_Device.TypeID = M_DeviceType.TypeID LEFT OUTER JOIN  M_User ON H_AlarmInfo.Deal = M_User.UserID WHERE DATEPART(YEAR,AlarmTime)='" + DateTime.Now.Year.ToString() + "' AND DATEPART(MONTH,AlarmTime)='" + DateTime.Now.Month.ToString() + "' AND DATEPART(DAY,AlarmTime)='" + DateTime.Now.Day.ToString() + "' ORDER BY H_AlarmInfo.AlarmTime DESC ";
            SqlDBHelper sdh = new SqlDBHelper();
            DataTable dtAlarm = sdh.GetTable(sql);
            dataGridView1.DataSource = dtAlarm;
            
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            string sql = "SELECT     H_AlarmInfo.AlarmID,H_AlarmInfo.AlarmTime, H_AlarmInfo.AlarmInfo, M_Device.DeviceID, M_Device.Name, M_Device.TypeID, M_Device.DevicePos, M_DeviceType.Name AS TypeName,H_AlarmInfo.Deal, M_User.Name AS UserName, H_AlarmInfo.DealTime FROM H_AlarmInfo LEFT OUTER JOIN M_Device ON H_AlarmInfo.DeviceID = M_Device.DeviceID LEFT OUTER JOIN M_DeviceType ON M_Device.TypeID = M_DeviceType.TypeID LEFT OUTER JOIN  M_User ON H_AlarmInfo.Deal = M_User.UserID WHERE AlarmTime BETWEEN '" + date1.Text + "' AND '" + date2.Text + "'";
            if (cbType.Text!=string.Empty)
            {
                switch(cbType.Text)
                {
                    case "火灾报警按钮": sql = sql + " AND M_Device.TypeID='FD'";break;
                    case "一氧化碳检测器": sql = sql + " AND M_Device.TypeID='COVI'"; break;
                    case "感温光纤": sql = sql + " AND M_Device.TypeID='Fiber'"; break;
                    case "紧急电话": sql = sql + " AND M_Device.TypeID='ET'"; break;
                    case "烟感器": sql = sql + " AND M_Device.TypeID='Somke'"; break;
                   
                }
            }
            if(!string.IsNullOrEmpty(cbDeal.Text))
            {
                switch(cbDeal.Text)
                {
                    case "是": sql = sql + " AND M_User.Name IS NOT NULL"; break;
                    case "否": sql = sql + " AND M_User.Name IS NULL"; break;
                }
            }
            sql = sql + " ORDER BY H_AlarmInfo.AlarmTime DESC ";
            SqlDBHelper sdh = new SqlDBHelper();
            DataTable dtAlarm = sdh.GetTable(sql);
            dataGridView1.DataSource = dtAlarm;
        }
        FireAlarmServicesClient.FireAlarmServicesClient fasc = new FireAlarmServicesClient.FireAlarmServicesClient();
        private void btConfirm_Click(object sender, EventArgs e)
        {
            if(dataGridView1.SelectedRows.Count==0)
            {
                return;
            }

            DialogResult dr = MessageBox.Show("用户[" + Global.User + "]，确定确认当前报警？", "确认报警信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dr == DialogResult.Yes)
            {
                string deviceId = dataGridView1.SelectedRows[0].Cells["DeviceID"].Value.ToString();
                string alarmInfo = dataGridView1.SelectedRows[0].Cells["AlarmInfo"].Value.ToString();
                string typeId = dataGridView1.SelectedRows[0].Cells["TypeID"].Value.ToString();
                string alarmid= dataGridView1.SelectedRows[0].Cells["AlarmID"].Value.ToString();

                string sql = "update H_AlarmInfo set Deal='" + Global.UserID + "',DealTime='" + DateTime.Now.ToString() + "' where AlarmID='" + alarmid + "'";
                bwSQL.RunWorkerAsync(sql);
                /*
                if (typeId == "FD" || typeId == "Somke" || typeId == "Fiber")
                {
                    try
                    {
                        fasc.ConfirmAlarm(deviceId, alarmInfo);
                    }
                    catch
                    {
                        MessageBox.Show("火灾报警传输服务未运行");
                    }
                }*/ 
            }
            btSearch_Click(btSearch, new EventArgs());
        }

        private void bwSQL_DoWork(object sender, DoWorkEventArgs e)
        {
            string sql = e.Argument as string;
            SqlDBHelper sdh = new SqlDBHelper();
            sdh.ExecuteCommand(sql);
        }
    }
}
